import Vue from 'vue';
import Vuex from 'vuex';
import { Toast } from 'vant';
import axios from '../axios';
import ls from '../util/localstorage';
import router from '../router/index.js';

Vue.use(Vuex)

const state = {
	userInfo: {},
	loadingObj: null,
  // [ '租借中', '已归还', '已撤销', '超时', '暂停计费' ]
  dict: {
    order: [
      { label: '租借中', value: 0 },
      { label: '已归还', value: 1 },
      { label: '已撤销', value: 2 },
      { label: '超时订单', value: 3 },
      { label: '暂停计费', value: 4 }
    ]
  }
}

const getters = {
  dictTrans(state) {
    let dictTrans = {};
    for (let key in state.dict) {
      dictTrans[key] = state.dict[key].dictTrans();
    }
    return dictTrans;
  },
  ifAdmin(state) {
    // 管理员
    return state.userInfo.roleType == 0 && state.userInfo.nick != '牛伟';
  },
  ifAgent() {
    // 代理
	  return state.userInfo.roleType == 1;
  },
  ifFirst() {
    // 一级代理
	  return state.userInfo.roleType == 1 && state.userInfo.role.creator.length == 5;
  },
  ifSecond() {
    // 二级代理
	  return state.userInfo.roleType == 1 && state.userInfo.role.creator.length > 5;
  },
  ifSales() {
    // 业务员
	  return state.userInfo.roleType == 2;
  },
  ifShoper() {
    // 商户
	  return state.userInfo.roleType == 3;
  },
  ifIntroducer() {
    // 服务商
	  return state.userInfo.roleType == 4;
  },
  ifDelay() {
    // 卖宝权限,卖宝可以看超时订单
	  return store.state.userInfo.role.delayingPurchases == 1;
  },
  ifTan() {
    // 弹出宝的权限
    return store.state.userInfo.userId == 10000 || (store.state.userInfo.roleType !== 0 && store.state.userInfo.role && store.state.userInfo.role.popupPower !== 0);
  }
}

const mutations = {
	setState(state, json) {
		for (let key in json) {
			state[key] = json[key];
		}
	},
	setUserInfo(state, value) {
		state.userInfo = value;
		ls.setItem('userInfo', value);
	},
  exit() {
    router.push('login');
  },
  getUserInfo() {
    state.userInfo = ls.getItem('userInfo');
  },
	loading(state, type) {
		if (type) {
			state.loadingObj = Toast.loading({
				duration: 0,
				forbidClick: true,
				message: '加载中...',
			});
		} else {
			if (state.loadingObj) {
				state.loadingObj.clear();
			}
		}
	}
}
'/api/chargerPlaEquipmentController/getChargerPlaEquipmentList?page=1&size=10&vague=0&userId=853257&equipmentNum=&content=&_=1657456598242'
'/api/chargerPlaEquipmentController/getChargerPlaEquipmentList?page=1&size=10&vague=0&userId=853257&equipmentNum=&content=&_=1657456157866'
const actions = {
}

// 注册上面引入的各大模块
const store = new Vuex.Store({
	state,
	getters,
	mutations,
	actions
})

export default store